package com.xhwl.centiotdata.action.visitor.impl;

import com.xhwl.centiotdata.action.visitor.IVisitorAuth;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationFactory;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.visitor.VisitorIntegrationConfigDTO;
import com.xhwl.centiotdata.service.integrateconfig.VisitorIntegrationConfig;
import com.xhwl.common.dto.cent.visitor.VisitorAuthDTO;
import com.xhwl.common.dto.cent.visitor.VisitorGroupListDTO;
import com.xhwl.common.dto.cent.visitor.VisitorInfoDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.visitor.Auth;
import com.xhwl.common.query.cent.visitor.VisitorQRCodeQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestPart;

import java.util.List;
import java.util.Objects;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 18:32 2022/3/3
 */
@Slf4j
@Service
public class VisitorAuthImpl implements IVisitorAuth {

    @Autowired
    VisitorIntegrationConfig visitorIntegrationConfig;


    @Override
    public ResultJson createVisitorAuth(VisitorAuthDTO dto) {
        //获取集成类
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(dto.getProductId());
        if(Objects.isNull(handler)){
            return ResultJson.fail("未找到处理方式");
        }
        //获取集成配置
        VisitorIntegrationConfigDTO integrationConfigDTO = getConfigDTO(dto.getProductId(),dto.getProjectId());
        if (integrationConfigDTO == null) {
            return ResultJson.fail("集成配置为空");
        }
        //处理
        return handler.createVisitorAuth(integrationConfigDTO, dto);
    }

    @Override
    public VisitorInfoDTO getVisitorInfoByAppointmentCode(VisitorAuthDTO dto) {
        //获取集成类
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(dto.getProductId());
        if(Objects.isNull(handler)){
            log.error("未找到处理方式");
            return null;
        }
        //获取集成配置
        VisitorIntegrationConfigDTO integrationConfigDTO = getConfigDTO(dto.getProductId(),dto.getProjectId());
        if (integrationConfigDTO == null) {
            log.error("集成配置为空");
            return null;
        }
        //处理
        String appointmentCode = dto.getAppointRecordId();
        return handler.getVisitorInfoByAppointmentCode(integrationConfigDTO,appointmentCode);
    }

    public VisitorInfoDTO getVisitorAccessRecord(VisitorAuthDTO dto){
        //获取集成类
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(dto.getProductId());
        if(Objects.isNull(handler)){
            log.error("未找到处理方式");
            return null;
        }
        //获取集成配置
        VisitorIntegrationConfigDTO integrationConfigDTO = getConfigDTO(dto.getProductId(),dto.getProjectId());
        if (integrationConfigDTO == null) {
            log.error("集成配置为空");
            return null;
        }
        return handler.getVisitorAccessRecord(integrationConfigDTO,dto);
    }

    @Override
    public ResultJson cancelVisitorAuth(VisitorAuthDTO visitorAuthDTO) {
        //获取集成类
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(visitorAuthDTO.getProductId());
        if(Objects.isNull(handler)){
            log.error("未找到处理方式");
            return ResultJson.fail("未找到处理方式");
        }
        //获取集成配置
        VisitorIntegrationConfigDTO integrationConfigDTO = getConfigDTO(visitorAuthDTO.getProductId(),visitorAuthDTO.getProjectId());
        if (integrationConfigDTO == null) {
            return ResultJson.fail("集成配置为空");
        }
        //处理
        return handler.removeVisitorAuth(integrationConfigDTO, visitorAuthDTO);
    }

    @Override
    public ResultJson getQrCode(VisitorAuthDTO visitorAuthDTO) {
        //获取集成类
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(visitorAuthDTO.getProductId());
        if(Objects.isNull(handler)){
            log.error("未找到处理方式");
            return ResultJson.fail("未找到处理方式");
        }
        //获取集成配置
        VisitorIntegrationConfigDTO integrationConfigDTO = getConfigDTO(visitorAuthDTO.getProductId(),visitorAuthDTO.getProjectId());
        if (integrationConfigDTO == null) {
            return ResultJson.fail("集成配置为空");
        }
        //处理
        return handler.getQrCode(integrationConfigDTO, visitorAuthDTO);
    }

    @Override
    public ResultJson createVrVisitor(VisitorAuthDTO visitorAuthDTO){
        //获取集成类
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(visitorAuthDTO.getProductId());
        if(Objects.isNull(handler)){
            return ResultJson.fail("未找到处理方式");
        }
        //获取集成配置
        VisitorIntegrationConfigDTO integrationConfigDTO = getConfigDTO(visitorAuthDTO.getProductId(),visitorAuthDTO.getProjectId());
        if (integrationConfigDTO == null) {
            return ResultJson.fail("集成配置为空");
        }
        // 推送访客数据给耀安VR
        try {
            handler.createVrVisitor(integrationConfigDTO,visitorAuthDTO);
        } catch (Exception e) {
            log.error("耀安VR推送访客数据异常{}",e);
        }
        return ResultJson.success();
    }

    @Override
    public ResultJson getVisitorAreaInfo(@RequestPart("visitorAuthDTO") VisitorAuthDTO visitorAuthDTO){
        //获取集成类
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(visitorAuthDTO.getProductId());
        if(Objects.isNull(handler)){
            return ResultJson.fail("未找到处理方式");
        }
        //获取集成配置
        VisitorIntegrationConfigDTO integrationConfigDTO = getConfigDTO(visitorAuthDTO.getProductId(),visitorAuthDTO.getProjectId());
        if (integrationConfigDTO == null) {
            return ResultJson.fail("集成配置为空");
        }
        return handler.getOpenDeviceInfo(integrationConfigDTO,visitorAuthDTO);
    }

    @Override
    public ResultJson<List<Auth>> getAuthDeviceListByOpenId(VisitorAuthDTO visitorAuthDTO){
        //获取集成类
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(visitorAuthDTO.getProductId());
        if(Objects.isNull(handler)){
            log.error("未找到处理方式");
            return ResultJson.fail("未找到处理方式");
        }
        //获取集成配置
        VisitorIntegrationConfigDTO integrationConfigDTO = getConfigDTO(visitorAuthDTO.getProductId(),visitorAuthDTO.getProjectId());
        if (integrationConfigDTO == null) {
            return ResultJson.fail("集成配置为空");
        }
        return handler.getAuthDeviceListByOpenId(integrationConfigDTO,visitorAuthDTO);
    }

    /**
     *
     * 获取集成配置
     * @param productId
     * @param projectId
     * @return
     */
    private VisitorIntegrationConfigDTO getConfigDTO(Integer productId, Integer projectId){
        List<VisitorIntegrationConfigDTO> integrationConfig = visitorIntegrationConfig.getIntegrationConfig(projectId);
        if(CollectionUtils.isEmpty(integrationConfig)){
            return null;
        }
        return integrationConfig.stream()
                .filter(visitorIntegrationConfigDTO -> visitorIntegrationConfigDTO.getProductId().equals(productId))
                .findFirst()
                .orElse(null);
    }
    @Override
    public ResultJson cancelVisitor(Integer projectId,String appointRecordId) {
        List<VisitorIntegrationConfigDTO> integrationConfig = visitorIntegrationConfig.getIntegrationConfig().get(projectId);
        VisitorIntegrationConfigDTO integrationConfigDTO = integrationConfig.get(0);
        log.info("integrationConfigDTO"+integrationConfigDTO);
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(integrationConfigDTO.getProductId());
        log.info("handler"+handler);
        VisitorInfoDTO infoDTO=new VisitorInfoDTO();
        infoDTO.setAppointRecordId(appointRecordId);
        return handler.cancelVisitor(integrationConfigDTO, infoDTO);

    }

    @Override
    public ResultJson leaveVisitor(Integer projectId,String openId) {
        List<VisitorIntegrationConfigDTO> integrationConfig = visitorIntegrationConfig.getIntegrationConfig().get(projectId);
        VisitorIntegrationConfigDTO integrationConfigDTO = integrationConfig.get(0);
        log.info("integrationConfigDTO"+integrationConfigDTO);
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(integrationConfigDTO.getProductId());
        log.info("handler"+handler);
        VisitorInfoDTO infoDTO=new VisitorInfoDTO();
        infoDTO.setOpenId(openId);
        return handler.leaveVisitor(integrationConfigDTO, infoDTO);

    }
    @Override
    public ResultJson visitorQRCode( Integer projectId,VisitorQRCodeQuery visitorQRCodeQuery) {
        List<VisitorIntegrationConfigDTO> integrationConfig = visitorIntegrationConfig.getIntegrationConfig().get(projectId);
        VisitorIntegrationConfigDTO integrationConfigDTO = integrationConfig.get(0);
        log.info("integrationConfigDTO"+integrationConfigDTO);
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(integrationConfigDTO.getProductId());
        log.info("handler"+handler);
        return handler.visitorQRCode(integrationConfigDTO, visitorQRCodeQuery);

    }
    @Override
    public List<VisitorGroupListDTO> pullVisitorPrivilegeGroup(Integer projectId,String groupName) {
        List<VisitorIntegrationConfigDTO> integrationConfig = visitorIntegrationConfig.getIntegrationConfig().get(projectId);
        VisitorIntegrationConfigDTO integrationConfigDTO = integrationConfig.get(0);
        log.info("integrationConfigDTO"+integrationConfigDTO);
        VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(integrationConfigDTO.getProductId());
        log.info("handler"+handler);
        return handler.pullVisitorPrivilegeGroup(integrationConfigDTO, groupName);

    }

}
